<?php

namespace app\admin\controller;

use app\common\controller\Admin;
use think\Request;
use app\index\model\ContentDesigns as designsModel;
use app\common\model\Type;

class Contentdesigns extends Admin {

    public $designsModel;
    public $typeModel;

    public function __construct(Request $request = null) {
        parent::__construct($request);
        $this->designsModel = new designsModel();
        $this->typeModel = new Type();
        $this->assign('setOrderUrl', url('contentdesigns/setorder'));
        $this->assign('score', config('score'));
        $this->assign('type', $this->typeModel->getValueByPid(1001));
        $this->assign('contentStatus', config('content_status'));
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index() {

        $search = $this->request->param();
        $where = [];

        if (isset($search['status']) && $search['status']) {
            $where[] = "`status` = {$search['status']}";
        }

        if (isset($search['title']) && trim($search['title'])) {
            $where[] = "title like '%{$search['title']}%'";
        }

        if (isset($search['add_time']) && $search['add_time']) {
            $startTime = strtotime($search['add_time']);
            $endTime = $startTime + 86399;
            $where[] = "(created_at between {$startTime} and {$endTime})";
        }

        if (isset($search['type']) && $search['type']) {
            $where[] = "type like '%-{$search['type']}-%'";
        }

        $whereStr = implode(' and ', $where);

        $list = $this->designsModel->where($whereStr)->order('list_order desc,id desc')->field('id,title,keywords,description,islink,display,thumb,list_order,created_at,updatet_at,type')->paginate(config('paginate.list_rows'), false, ['query' => $search]);
        $this->assign('list', $list);
        $this->assign('search', $search);
        return $this->fetch('designs/index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function addDesigns() {

        if ($this->request->isPost()) {
            $param = $this->_getDesignsParam();
            if ($param['id']) {
                $this->designsModel->where('id', $param['id'])->update($param);
            } else {
                $param['created_at'] = time();
                $this->designsModel->insert($param);
            }
            $this->success('操作成功');
            exit;
        }
        $this->assign('option', 1);
        $series = $this->typeModel->getValueByPid(1051);
        $series[0] = '无';
        ksort($series);
        $this->assign('series', $series);

        return $this->fetch('designs/edit_designs');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function editDesigns($id) {

        if ($this->request->isPost()) {
            $param = $this->_getDesignsParam();
            if ($param['id']) {
                $this->designsModel->where('id', $param['id'])->update($param);
            } else {
                $param['created_at'] = time();
                $this->designsModel->insert($param);
            }
            $this->success('操作成功');
            exit;
        }

        $info = $this->designsModel->find($id);
        $info['type'] = explode('-', trim($info['type'], '-'));
        $info['multi_file'] = json_decode($info['multi_file'], true);
        $this->assign('info', $info);

        $this->assign('option', 2);
        $series = $this->typeModel->getValueByPid(1051);
        $series[0] = '无';
        ksort($series);
        $this->assign('series', $series);

        return $this->fetch('designs/edit_designs');
    }

    /**
     * 删除
     * @param type $id
     */
    public function delDesigns($id) {
        $this->designsModel->where('id', $id)->delete();
        $this->success('删除成功');
    }

    /**
     * 更新排序
     */
    public function setOrder() {
        $param = $this->request->param();

        $this->_setorder($param['list_order'], 'content_designs');

        $this->success('排序成功');
    }

    /**
     * 提取表单内容
     * @return type
     */
    private function _getDesignsParam() {
        $param = $this->request->param();

        //多文件
        $multi = [];
        if (isset($param['multi_file']) && $param['multi_file']) {
            foreach ($param['multi_file']['path'] as $key => $value) {
                $path = trim($value);
                $tmp = [];
                if ($path) {
                    $tmp['order'] = $param['multi_file']['order'][$key];
                    $tmp['path'] = $path;
                    $tmp['title'] = $param['multi_file']['title'][$key];
                    $multi[] = $tmp;
                } else {
                    unset($param['multi_file']['order'][$key]);
                }
            }
        }
        if (!empty($multi)) {
            array_multisort($param['multi_file']['order'], SORT_ASC, $multi);
        }
        $param['multi_file'] = json_encode($multi);

        //风格
        $type = null;
        if (isset($param['type'])) {
            $type = '-' . implode('-', $param['type']) . '-';
        }
        $param['type'] = $type;

        return $param;
    }

}
